const express=require("express");
const router = require("./routes/05_birds");
const app=express();
const port=3001;
app.use(express.static("public"));
app.use("/public",express.static("public"));
app.use(express.json());
app.use(express.urlencoded({extended:false}));
app.use(function(req,res,next){
    console.log(`应用层中间件:Url-${req.url},Time-${new Date().toLocaleString()}`);
    next();
    
});
app.use("/mw/:id",function(req,res,next){
    console.log(`指定路径应用层中间件，http请求方法${req.method}`);
    next();

});
router.use(function(req,res,next){
    console.log(`路由层中间件:Url-${req.url},Time-${new Date().toLocaleString()}`)
  
    
});
router.get("/router_mw/:id",function(req,res,next){
    console.log(`路由层中间件，路由参数${req.params.id}`);
    res.render("渲染");

});
app.use(router);
app.get("/test-error",function(){
    throw new Error("Oops!");
});
app.use(function(err,req,res,next){
    console.error(err.message);
    res.status(500).send("服务区500错误！");
});
app.use("*",function(req,res){
    console.log("404 Not Found");
    res.json({
        status:400,
        title:"页面不存在"
    });

});
app.listen(port,function(){
    console.log(`MyApp访问地址:http://127.0.0.1:${port}`);
});